from rest_framework import serializers

from apps.goods.models import SKU
from apps.orders.models import OrderInfo, OrderGoods


class SKUSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ('name', 'default_image')


class OrderGoodsSerialzier(serializers.ModelSerializer):
    # 订单商品表关联sku商品表进行返回
    sku = SKUSerializer()

    class Meta:
        model = OrderGoods
        fields = ('count', 'price', 'sku')


class OrderInfoSerialzier(serializers.ModelSerializer):
    """"
    skus": [
            {
                "count": 1,
                "price": "6499.00",
                "sku": {
                    "name": "Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机",
                    "default_image_url": "http://image.meiduo.site:8888/group1/M00/00/02/CtM3BVrRZCqAUxp9AAFti6upbx41220032"
                }
            },
        ]
    """
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()

    # 主表嵌套副表返回  订单表是主表  订单商品表是副表  skus最为副表的序列化器字段  单独定义订单商品表序列化器指定多个字段进行嵌套返回
    skus = OrderGoodsSerialzier(many=True)

    class Meta:
        model = OrderInfo
        fields = '__all__'
